package com.cc2019.buke.type.frag;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cc2019.buke.R;

import java.util.ArrayList;
import java.util.List;


public class TypeFragment extends Fragment implements View.OnClickListener{
    private View view;
    private RadioGroup radioGroup;
    private Context context;
    private Button zbutton;
    private Button wbutton;
    private Button xbutton;
    private Button obutton;

    private LayoutInflater inflater;
    public TypeFragment(){}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_type, container, false);
        radioGroup=(RadioGroup) view.findViewById(R.id.typegroup);
//        if(intent==null){
//            radioGroup.check(R.id.radio_china);
//        }

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        inflater=LayoutInflater.from(getActivity());

//        radioGroup=(RadioGroup)getActivity().findViewById(R.id.typegroup);
        zbutton=(RadioButton)getActivity().findViewById(R.id.radio_china);
        wbutton=(RadioButton)getActivity().findViewById(R.id.radio_waiguo);
        xbutton=(RadioButton)getActivity().findViewById(R.id.radio_afraid);
        obutton=(RadioButton)getActivity().findViewById(R.id.radio_other);
        zbutton.setOnClickListener(this);
        wbutton.setOnClickListener(this);
        xbutton.setOnClickListener(this);
        obutton.setOnClickListener(this);
        radioGroup.check(0);

        setDefaultFragment();
    }

    private void setDefaultFragment(){
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        TypeCFragment typeCFragment=new TypeCFragment();
        transaction.add(R.id.framelayout,typeCFragment).commit();
    }

    @Override
    public void onClick(View v) {

        FragmentManager fm = getFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();

        switch (v.getId()) {
            case R.id.radio_china:
                transaction.replace(R.id.framelayout, new TypeCFragment());
                break;
            case R.id.radio_waiguo:
                transaction.replace(R.id.framelayout, new TypeWFragment());
                break;
            case R.id.radio_afraid:
                transaction.replace(R.id.framelayout, new TypeXFragment());
                break;
            case R.id.radio_other:
                transaction.replace(R.id.framelayout, new TypeOFragment());
                break;

        }

            transaction.commit();

    }


}


